1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package mraz.com.palettedemo;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.graphics.Palette; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity { private final int[] resIds = {R.drawable.p_1, R.drawable.p_2, R.drawable.p_3, R.drawable.p_4, R.drawable.p_5, R.drawable.p_6, R.drawable.p_7}; private final String[] titles = {"Vibrant", "DarkVibrant", "LightVibrant", "Muted", "DarkMuted", "LightMuted"}; private Toolbar toolbar; private ArrayList<Integer> colorList; private int clickCount = 0;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
colorList = new ArrayList<>(); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_content); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter();
recyclerView.setAdapter(myRecyclerAdapter); recyclerView.setLayoutManager(layoutManager);
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { int offset = recyclerView.computeHorizontalScrollOffset(); int width = recyclerView.getChildAt(0).getWidth(); int current = offset / width; int secondoffset = offset % width; if (secondoffset >= width / 2) { current = current + 1; } setActionBarColor(current); clickCount = 0; super.onScrolled(recyclerView, dx, dy); } }); setActionBarColor(0); }
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); }
private void setActionBarColor(int position) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resIds[position]); Palette.PaletteAsyncListener paletteAsyncListener = new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { colorList.clear(); colorList.add(palette.getVibrantColor(Color.WHITE)); colorList.add(palette.getDarkVibrantColor(Color.WHITE)); colorList.add(palette.getLightVibrantColor(Color.WHITE)); colorList.add(palette.getMutedColor(Color.WHITE)); colorList.add(palette.getDarkMutedColor(Color.WHITE)); colorList.add(palette.getLightMutedColor(Color.WHITE)); toolbar.setBackgroundColor(colorList.get(0)); toolbar.setTitle(titles[0]); } }; Palette.from(bitmap).generate(paletteAsyncListener); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: { clickCount++; int index = clickCount % (colorList.size()); toolbar.setBackgroundColor(colorList.get(index)); toolbar.setTitle(titles[index]); break; } case R.id.action_del: { if (clickCount > 0) clickCount--; int index = clickCount % (colorList.size()); toolbar.setBackgroundColor(colorList.get(index)); toolbar.setTitle(titles[index]); break; } } return super.onOptionsItemSelected(item); } }
|